﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class ChatUserDao
    {
        public bool Exit(SqlConnection conn, string userId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT [dbo].[Chat_Messages] ");
            builder.Append("( ");
            builder.Append("\t[SessionId],\t\t ");
            builder.Append("\t[Color], ");
            builder.Append("\t[IsPrivate], ");
            builder.Append("\t[Content], ");
            builder.Append("\t[SendTime] ");
            builder.Append(") ");
            builder.Append("SELECT  ");
            builder.Append("\t[SessionId],\t\t ");
            builder.Append("\t'black', ");
            builder.Append("\t0, ");
            builder.Append("\t[NickName] + '已经退出聊天室', ");
            builder.Append("\tGETDATE() ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Chat_User] ");
            builder.Append("WHERE ");
            builder.Append("\t[UserId] = @UserId ");
            builder.Append("DELETE FROM [dbo].[Chat_User] ");
            builder.Append("WHERE ");
            builder.Append("\t[UserId] = @UserId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public bool Insert(SqlConnection conn, ChatUserData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF NOT EXISTS(SELECT [SessionId] FROM [dbo].[Chat_User] WHERE [SessionId]=@SessionId AND [UserId]=@UserId) ");
            builder.Append("\tINSERT [dbo].[Chat_User] ");
            builder.Append("\t( ");
            builder.Append("\t    [SessionId], ");
            builder.Append("\t    [UserId], ");
            builder.Append("\t    [NickName], ");
            builder.Append("\t    [LastRefreshTime], ");
            builder.Append("\t    [LastMessageId], ");
            builder.Append("                 [State] ");
            builder.Append("\t) ");
            builder.Append("\tVALUES ");
            builder.Append("\t( ");
            builder.Append("\t    @SessionId, ");
            builder.Append("\t    @UserId, ");
            builder.Append("\t    @NickName, ");
            builder.Append("\t    GETDATE(), ");
            builder.Append("\t    0, ");
            builder.Append("\t     1 ");
            builder.Append("\t) ");
            builder.Append("ELSE ");
            builder.Append("\tUPDATE [dbo].[Chat_User] ");
            builder.Append("\tSET ");
            builder.Append("\t\t[UserId] = @UserId, ");
            builder.Append("\t\t[NickName] = @NickName, ");
            builder.Append("\t\t[LastRefreshTime] = GETDATE(), ");
            builder.Append("\t\t[State] = 1 ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[SessionId]=@SessionId AND [UserId]=@UserId ");
            SqlParameter[] commandParameters = new SqlParameter[5];
            commandParameters[0] = new SqlParameter("@SessionId", SqlDbType.Int);
            commandParameters[0].Value = data.SessionId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = data.UserId;
            commandParameters[2] = new SqlParameter("@NickName", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = data.NickName;
            commandParameters[3] = new SqlParameter("@LastRefreshTime", SqlDbType.DateTime);
            commandParameters[3].Value = data.LastRefreshTime;
            commandParameters[4] = new SqlParameter("@LastMessageId", SqlDbType.Int);
            commandParameters[4].Value = data.LastMessageId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public DataTable SelectOnline(SqlConnection conn, int sessionId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("UPDATE [dbo].[Chat_User] ");
            builder.Append("SET ");
            builder.Append("\t[State]=0 ");
            builder.Append("WHERE ");
            builder.Append("\tDATEDIFF(s,[LastRefreshTime],getdate())>120 ");
            builder.Append("SELECT ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[NickName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Chat_User] ");
            builder.Append("WHERE ");
            builder.Append("\t[SessionId] = @SessionId ");
            builder.Append("\tAND [State]=1 ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@SessionId", SqlDbType.Int) };
            commandParameters[0].Value = sessionId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Chat_User";
            return set.Tables[0];
        }
    }
}

